package com.hotel.BL;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.SharedCache;
import com.StatusTitle;
import com.Util;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Block;
import data.CleaningStatus;
import data.Floor;
import data.Reservation;
import data.Room;
import data.RoomType;
import data.passingData.RoomData;

public class RoomLogic extends AbstractBL {
	
	public RoomLogic(HttpServletRequest req){
		this.req = req;
	}
	
	public RoomLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	
	public List<Room> GenerateRoom(long hotelId) {
		List<Room> list = new ArrayList<Room>();

		RoomTypeLogic roomTypeLogic = new RoomTypeLogic(req,resp);

		RoomType iSuite = roomTypeLogic.GetRoomType(1, "iSuite");
		RoomType iDeluxe = roomTypeLogic.GetRoomType(1, "iDeluxe King Bed");
		RoomType iDeluxeQ = roomTypeLogic.GetRoomType(1, "iDeluxe Queen Bed");
		
		BlockLogic blockLogic = new BlockLogic(req,resp);
		Block block1 = blockLogic.GetBlock(1, "block1");

		GenericLogic genericLogic = new GenericLogic(req,resp);

		CleaningStatus clean = genericLogic.GetCleaningStatus(1,
				StatusTitle.CleaningStatusTitle.CleanRooms);
		CleaningStatus dirty = genericLogic.GetCleaningStatus(1,
				StatusTitle.CleaningStatusTitle.DirtyRooms);

		Room r = new Room();
		if(GetRoom(hotelId,"211") == null){
			r = new Room();
			r.setRoomtype(iSuite);
			r.setRoomName("211");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"107") == null){
			r = new Room();
			r.setRoomtype(iDeluxe);
			r.setRoomName("107");
			r.setCleaningStatus(dirty);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"110") == null){
			r = new Room();
			r.setRoomtype(iDeluxe);
			r.setRoomName("110");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"111") == null){
			r = new Room();
			r.setRoomtype(iDeluxe);
			r.setRoomName("111");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"112") == null){
			r = new Room();
			r.setRoomtype(iDeluxe);
			r.setRoomName("112");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"208") == null){
			r = new Room();
			r.setRoomtype(iDeluxe);
			r.setRoomName("208");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"209") == null){
			r = new Room();
			r.setRoomtype(iDeluxe);
			r.setRoomName("209");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"101") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("101");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"102") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("102");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"103") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("103");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"104") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("104");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"106") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("106");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"113") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("113");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"114") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("114");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"115") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("115");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"116") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("116");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		if(GetRoom(hotelId,"117") == null){
			r = new Room();
			r.setRoomtype(iDeluxeQ);
			r.setRoomName("117");
			r.setCleaningStatus(clean);
			r.setHotelId(hotelId);
			r.setBlock(block1);
			list.add(r);
		}
		return list;
	}

	public Room GetRoom(long id) {
		Key key = KeyFactory.createKey(EntityName.Room, id);
		try {
			Entity lang = datastore.get(key);
			Room c = new Room();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public Room GetRoom(long hotelId, String roomName) {
		Query q = new Query(EntityName.Room);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterRoomName = new FilterPredicate("roomName", FilterOperator.EQUAL,
				roomName);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable, filterRoomName);

		q.setFilter(filter);

		return GetRoom(datastore.prepare(q).asSingleEntity());
	}

	public List<Room> GetAllRoom(long hotelId) {
		List<Room> list = GetRoomWithCache(hotelId);
		return FillterRoom(list);
	}
	
	public List<Room> FillterRoom(List<Room> list){
		for(int i=0;i< list.size();i++){
			boolean blockStatus = true;
			boolean floorStatus = true;
			boolean roomtypeStatus = list.get(i).getRoomtype().isStatus();
			if(list.get(i).getBlockString() != null){
				blockStatus = list.get(i).getBlock().getStatus();
			}
			
			if(list.get(i).getFloorString() != null){
				floorStatus = list.get(i).getFloor().isStatus();
			}
			
			if(!blockStatus || !floorStatus || !roomtypeStatus){
				list.remove(i);
				i--;
			}
		}
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<Room> GetRoomWithCache(long hotelId){
		if(SharedCache.getHotelCache(hotelId, "RoomLogic.GetRoom") != null){
			List<Room> rs = (List<Room>) SharedCache.getHotelCache(hotelId, "RoomLogic.GetRoom");
			List<Room> result = new ArrayList<Room>(rs);
			return result;
		}
		else{
			List<Room> list = GetAvaliableRoom(hotelId);
			SharedCache.setHotelCache(hotelId, "RoomLogic.GetRoom",list,3600);
			List<Room> result = new ArrayList<Room>(list);
			return result;
		}
	}

	public List<Room> GetAvaliableRoom(long hotelId) {
		Query q = new Query(EntityName.Room);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable);

		q.setFilter(filter);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public long GetRoomByBlockId(long hotelId,long blockId) {
		Query q = new Query(EntityName.Room);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterBlock = new FilterPredicate("block",
				FilterOperator.EQUAL, String.valueOf(blockId));

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable,filterBlock);

		q.setFilter(filter);

		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults())).size();
	}

	public long GetRoomByFloorId(long hotelId,long floorId) {
		Query q = new Query(EntityName.Room);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterFloor = new FilterPredicate("floor",
				FilterOperator.EQUAL, String.valueOf(floorId));

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable,filterFloor);

		q.setFilter(filter);

		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults())).size();
	}
	
	@SuppressWarnings("unchecked")
	public List<Room> GetRoomWithCache(long hotelId,long roomtype){
		List<Room> rs = (List<Room>)SharedCache.getHotelCache(hotelId, "RoomLogic.GetRoomFromRoomType."+roomtype);
		if(rs != null){
			List<Room> result = new ArrayList<Room>(rs);
			return result;
		}
		else{
			List<Room> list = GetRoom(hotelId, roomtype);
			SharedCache.setHotelCache(hotelId, "RoomLogic.GetRoomFromRoomType."+roomtype,list,3600);
			
			List<Room> result = new ArrayList<Room>(list);
			return result;
		}
	}
	public List<Room> GetRoom(long hotelId,long roomtype) {
		Query q = new Query(EntityName.Room);
		
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterRoomtype = new FilterPredicate("roomType",
				FilterOperator.EQUAL, String.valueOf(roomtype));
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);

		Filter filter = CompositeFilterOperator.and(filterHotel,filterRoomtype,filterStatus,
				filterAvaliable);

		q.setFilter(filter);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
		
	public Room GetRoom(Entity entity) {
		if(entity == null) return null;
		Room item = new Room();
		item.setEntity(entity);
		return item;
	}

	public List<Room> GetList(List<Entity> entities) {
		List<Room> list = new ArrayList<Room>();
		if(entities!=null)
		for (Entity e : entities) {
			Room a = new Room();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public List<List<List<Room>>> GetRoomGroupByRoomType(List<Room> list) {
		List<List<List<Room>>> groupList = new ArrayList<List<List<Room>>>();

		for (Room r : list)
			groupList = GroupRoom(groupList, r);
		return groupList;
	}

	private List<List<List<Room>>> GroupRoom(List<List<List<Room>>> groupList, Room r) {
		boolean isExist = false;
		for(List<List<Room>> block : groupList)
		{
			if(isExist)
				break;
			boolean existingType = false;
			//correct Block
			if(block != null && block.size() > 0 &&
				block.get(0) != null && block.get(0).size() > 0 &&
				block.get(0).get(0).getBlock().getId() == r.getBlock().getId())
			{
					for(List<Room> type : block)
					{
						//correct Type
						if(type != null && type.size() > 0 &&
							type.get(0).getRoomtype().getId() == r.getRoomtype().getId())
						{
							isExist = true;
							existingType = true;
							type.add(r);
							
							break;
						}
					}
					if(!existingType)
					{
						//correctBlock but new type
						isExist = true;
						
						List<Room> type = new ArrayList<Room>();
						type.add(r);
						block.add(type);
						
						break;
					}
			}
		}
		
		if(!isExist)
		{
			List<Room> type = new ArrayList<Room>();
			type.add(r);
			
			List<List<Room>> block = new ArrayList<List<Room>>();
			block.add(type);
			
			groupList.add(block);
		}
		
		return groupList;
	}

	public Room AddRoom(long hotelId,String roomName,String roomtypeString,String floorString,String blockString,String comment,String cleaningStatusString,long sortOrder) throws EntityNotFoundException{
		Room r = new Room();
		r.setHotelId(hotelId);
		r.setRoomName(roomName);
		r.setRoomtypeString(roomtypeString);
		r.setFloorString(floorString);
		r.setBlockString(blockString);
		r.setComment(comment);
		r.setCleaningStatusString(cleaningStatusString);
		r.setSortOrder(sortOrder);
		r.setAvaliable(true);
		r.setStatus(true);
		Key k = datastore.put(r.getEntity());
		r.setId(k.getId());
		r.setKey(k);

		return r;
	}
	
	public Room EditRoom(long roomId,String roomName,String roomtypeString,String floorString,String blockString,String comment) throws EntityNotFoundException{
		Room r = GetRoom(roomId);
		r.setRoomName(roomName);
		r.setRoomtypeString(roomtypeString);
		r.setFloorString(floorString);
		r.setBlockString(blockString);
		r.setComment(comment);

		Key k = datastore.put(r.getEntity());
		r.setId(k.getId());
		r.setKey(k);

		return r;
	}
	
	public Room DeleteRoom(long roomId) throws EntityNotFoundException{
		Room r = GetRoom(roomId);
		r.setAvaliable(false);
		r.setStatus(false);

		Key k = datastore.put(r.getEntity());
		r.setId(k.getId());
		r.setKey(k);

		return r;

	}

	public Room ChangeStatus(long roomId) throws EntityNotFoundException{
		
		Room a = GetRoom(roomId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}

		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}	
	
	public Room ChangeCleaningStatus(long roomId,long cleaningStatus) throws EntityNotFoundException{
		Room a = GetRoom(roomId);
		a.setCleaningStatusString(String.valueOf(cleaningStatus));
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;	
	}
	
	public List<CleaningStatus> GetCleaningStatus(long hotelId){
		Query q = new Query(EntityName.CleaningStatus);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		
		q.setFilter(filterHotel);
		
		return GetListCleaningStatus(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	public List<CleaningStatus> GetListCleaningStatus(List<Entity> entities) {
		List<CleaningStatus> list = new ArrayList<CleaningStatus>();
		if(entities!=null)
		for (Entity e : entities) {
			CleaningStatus a = new CleaningStatus();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public Room ChangeBlockData(long roomId,long blockId) throws EntityNotFoundException{
		Room a = GetRoom(roomId);
		BlockLogic blockLogic = new BlockLogic(req,resp);
		Block block = blockLogic.GetBlock(blockId);
		a.setBlock(block);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public List<Room> GetAllRoom4AdminLimit(long hotelId,long offset,long line,String byRoomType,String byBlock,String byFloor)
	{
		Query q = new Query(EntityName.Room);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterRoomType = new FilterPredicate("roomType",FilterOperator.EQUAL,byRoomType);
		Filter filterBlock = new FilterPredicate("block",FilterOperator.EQUAL,byBlock);
		Filter filterFloor = new FilterPredicate("floor",FilterOperator.EQUAL,byFloor);
		
		Filter filter ;
		if(byRoomType.equals("All") && byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterRoomType);
			q.setFilter(filter);
		}else if(byRoomType.equals("All") && !byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterBlock);
			q.setFilter(filter);
		}else if(byRoomType.equals("All") && byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterFloor);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && !byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterRoomType,filterBlock);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterRoomType,filterFloor);
			q.setFilter(filter);
		}else if(byRoomType.equals("All") && !byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterBlock,filterFloor);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && !byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterBlock,filterFloor,filterRoomType);
			q.setFilter(filter);
		}
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}	
	
	public long AllRoomSize(long hotelId,String byRoomType,String byBlock,String byFloor)
	{
		Query q = new Query(EntityName.Room);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterRoomType = new FilterPredicate("roomType",FilterOperator.EQUAL,byRoomType);
		Filter filterBlock = new FilterPredicate("block",FilterOperator.EQUAL,byBlock);
		Filter filterFloor = new FilterPredicate("floor",FilterOperator.EQUAL,byFloor);
		
		Filter filter ;
		if(byRoomType.equals("All") && byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterRoomType);
			q.setFilter(filter);
		}else if(byRoomType.equals("All") && !byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterBlock);
			q.setFilter(filter);
		}else if(byRoomType.equals("All") && byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterFloor);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && !byBlock.equals("All") && byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterRoomType,filterBlock);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterRoomType,filterFloor);
			q.setFilter(filter);
		}else if(byRoomType.equals("All") && !byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterBlock,filterFloor);
			q.setFilter(filter);
		}else if(!byRoomType.equals("All") && !byBlock.equals("All") && !byFloor.equals("All")){
			filter =  CompositeFilterOperator.and(filterHotel, filterAvaliable,filterBlock,filterFloor,filterRoomType);
			q.setFilter(filter);
		}
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults())).size();
	}
	
	public List<Room> GetAllRoom4Admin(long hotelId)
	{
		Query q = new Query(EntityName.Room);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public Room EditSortOrder(long id,long sortOrder) throws EntityNotFoundException{
		
		Room a = GetRoom(id);
		a.setSortOrder(sortOrder);
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public HashMap<String,List<Reservation>> GetRoomReservationTodayStatusByList(long hotelId)
	{
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		HashMap<String,List<Reservation>> list = new HashMap<String,List<Reservation>>();
		list.put("checkIn",resLogic.GetCheckinToday(hotelId));
		list.put("occupied",resLogic.GetStayToday(hotelId));
		list.put("checkout",resLogic.GetCheckoutToday(hotelId));
		list.put("DNR", resLogic.GetDNRToday(hotelId));
		return list;
	}
	
	public List<Reservation> GetRoomReservationTodayStatus(HashMap<String,List<Reservation>> list)
	{
		List<Reservation> rs = new ArrayList<Reservation>();
		rs.addAll(list.get("checkIn"));
		rs.addAll(list.get("occupied"));
		rs.addAll(list.get("DNR"));
		//rs.addAll(list.get("checkout"));
		return rs;
	}
	
	public List<Reservation> GetRoomReservationYesterdayStatus(long hotelId)
	{
		GenericLogic gLogic = new GenericLogic(req,resp);
		Date d = Util.GetOnlyDate(gLogic.ServerDateFromNightAudit(hotelId));
		d = Util.addDays(d, -1);
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		List<Reservation> list = new ArrayList<Reservation>();
		list.addAll(resLogic.GetCheckinSpecificDate(hotelId,d));
		list.addAll(resLogic.GetStaySpecificDate(hotelId,d));
		return list;
	}
	
	public Room UpdateRoom(Room room)
	{
		Key key = datastore.put(room.getEntity());
		room.setId(key.getId());
		room.setKey(key);
		return room;
	}
	
	public List<Object> loadFilter(long hotelId){
		List<RoomType> roomtype = new RoomTypeLogic(req,resp).GetAllRoomType(hotelId);
		List<Block> block = new BlockLogic(req,resp).GetAllBlock(hotelId);
		List<Floor> floor = new FloorLogic(req,resp).GetAllFloor(hotelId);
		
		List<Object> obj = new ArrayList<Object>();
		obj.add(roomtype);
		obj.add(block);
		obj.add(floor);
		
		return obj;
	}
	
	public List<RoomData> toRoomDataList(List<Room> obj){
		List<RoomData> objDataList = new ArrayList<RoomData>();
		for(int i =0;i< obj.size();i++){
			RoomData objData = new RoomData();
			objData.setBlockName(obj.get(i).getBlock().getTitle());
			if(obj.get(i).getFloorString()!= null ){
				objData.setFloorName(obj.get(i).getFloor().getTitle());
			}else{
				objData.setFloorName(null);
			}
			
			objData.setId(obj.get(i).getId());
			objData.setRoomName(obj.get(i).getRoomName());
			objData.setRoomtypeName(obj.get(i).getRoomtype().getShortName());
			objData.setStatus(obj.get(i).isStatus());
			objDataList.add(objData);
		}
		return objDataList;
	}
	
	public boolean CanChangeRoom(long hotelId,long roomId){
		Query q = new Query(EntityName.Reservation);
		Date date = new NightAuditLogic(req,resp).ChangeToNightAuditTime(hotelId,new GenericLogic(req,resp).ServerDateTime());
		Filter filterhotelId = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterDate = new FilterPredicate("dateTo",
				FilterOperator.GREATER_THAN_OR_EQUAL, date);
		Filter filter = CompositeFilterOperator.and(filterhotelId, filterDate);
		q.setFilter(filter);
		List<Reservation> list = new ReservationLogic(req,resp).GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
		boolean isFoundUsed = false;
		for(int i=0;i<list.size();i++){
			if(list.get(i).getRoom().getId() == roomId){
				isFoundUsed = true;
				break;
			}
		}
		return isFoundUsed;
	}
}
